package com.minhua.jijiehao2.mainframe.mine.mineFragment.impl

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseFragment
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity
import com.minhua.jijiehao2.mainframe.mine.mineFragment.imp.IMineView
import com.minhua.jijiehao2.mainframe.mine.problemactivity.impl.ProblemActivity
import com.minhua.jijiehao2.mainframe.mine.productListActivity.impl.ProductListActivity
import com.minhua.jijiehao2.mainframe.mine.usercenteractivity.impl.UserCenterActivity
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.fragment_mine.*

class MineFragment : BaseFragment<IMineView, MinePresenter>(), IMineView {
    override fun getLayoutId(): Int = R.layout.fragment_mine

    override fun getPresenter(): MinePresenter = MinePresenter()

    companion object {

        fun newInstance(): MineFragment {
            val fragment = MineFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.tvRight, R.id.userCenter ->
                if (LoginActivity.logged(activity)) {
                    startActivity(Intent(activity, UserCenterActivity::class.java))
                }
            R.id.problem -> startActivity(Intent(activity, ProblemActivity::class.java))
            R.id.call -> {
                if (!TextUtils.isEmpty(servicePhone)) {
                    dialog?.setTitle("客服热线")
                    dialog?.setMessage(servicePhone)
                    dialog?.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", { dialog, _ -> dialog.dismiss() })
                    dialog?.setButton(AlertDialog.BUTTON_POSITIVE, "确定", { dialog, _ ->
                        val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + servicePhone))
                        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                        startActivity(intent)
                        dialog.dismiss()
                    })
                    dialog?.show()
                } else {
                    T.showShort("客服热线请求中")
                    mPresenter.getPhone()
                }
            }
            R.id.tvName, R.id.ivIcon -> LoginActivity.logged(activity)
            R.id.products -> if (LoginActivity.logged(activity)) {
                startActivity(Intent(activity, ProductListActivity::class.java))
            }
        }
    }

    private var isFirstIn = true
    private var dialog: AlertDialog? = null

    override fun initView() {
        ctTitle.viewHolder.setOnClickListener(R.id.tvRight, mOnClickListener)
        tvName.setOnClickListener(mOnClickListener)
        ivIcon.setOnClickListener(mOnClickListener)
        userCenter.setOnClickListener(mOnClickListener)
        problem.setOnClickListener(mOnClickListener)
        call.setOnClickListener(mOnClickListener)
        products.setOnClickListener(mOnClickListener)
        dialog = AlertDialog.Builder(activity).create()
        setData()
    }

    override fun onResume() {
        super.onResume()
        if (isFirstIn) {
            isFirstIn = !isFirstIn
            mPresenter.getPhone()
        } else {
            setData()
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
//            initData()
            setData()
        }
    }

    private fun setData() {
        val logged = SPUtils.get(DefineKey.LOGIN_STATE, false) as Boolean
        if (logged) {
            var nickName = SPUtils.get(DefineKey.USER_NICK_NAME, "") as String
            if (TextUtils.isEmpty(nickName)) {
                nickName = SPUtils.get(DefineKey.USER_NAME, "user") as String + SPUtils.get(DefineKey.USER_ID, "user")
            }
            tvName.text = nickName
            SPUtils.put(DefineKey.USER_NICK_NAME, nickName)
            val userIcon = SPUtils.get(DefineKey.USER_ICON, "") as String
            if (!TextUtils.isEmpty(userIcon)) {
                Glide.with(activity)
                        .load(userIcon)
                        .placeholder(R.mipmap.default_head_icon)
                        .error(R.id.default_activity_button)
                        .into(ivIcon)
            }
        } else {
            tvName.text = "登录/注册"
            ivIcon.setImageResource(R.mipmap.default_head_icon)
        }
    }


    override fun initData() {
        val logged = SPUtils.get(DefineKey.LOGIN_STATE, false) as Boolean
        if (logged) {
            mPresenter.getUserInfo()
        }
    }

    private var servicePhone: String? = null
    override fun onPhoneSuccess(phone: String) {
        servicePhone = phone
        SPUtils.put(DefineKey.SERVICE_PHONE, phone)
    }


    override fun onUserInfoSuccess() {
        setData()
    }
}
